// axios二次封装
import axios from "axios";
import { Message } from "element-ui";
// import nprogress from "nprogress";
// import "nprogress/nprogress.css"


const requests = axios.create({
    baseURL: '/wsw',
    timeout: 5000
})

requests.interceptors.request.use(config => {
    // nprogress.start()
    return config
}, error => {
    return Promise.reject(error)
})

requests.interceptors.response.use(response => {
    // nprogress.done()
    const res = response.data
    if (res.code == 2000) {
        return res
    } else if (res.code >= 3000 && res.code < 4000) {
        Message({
            message: res.msg || 'Error',
            type: 'info'
        })
        Promise.reject(new Error(res.message || 'Error'))
    } else if (res.code >= 4000) {
        Message({
            message: res.msg || 'Error',
            type: 'error'
        })
        return Promise.reject(new Error(res.message || 'Error'))
    }
}, error => {
    return Promise.reject(error)
})

export default requests